/*
 * Copyright (C) 2019 Baidu, Inc. All Rights Reserved.
 */
package com.bootdo.hjh.dao.provider;

import java.util.Date;
import java.util.Map;

public class StockProvider {

    public String listStocks(Map<String, Object> map) {
        Integer offset = (Integer) map.get("offset");
        Integer limit = (Integer) map.get("limit");
        Integer type = (Integer) map.get("type");
        Date beginDate = (Date) map.get("beginDate");
        Date endDate = (Date) map.get("endDate");
        Long userId = (Long) map.get("userId");
        StringBuilder sb = new StringBuilder();
        sb.append("select * from t_stock where 1>0 ");
        if (type != null && type > 0) {
            sb.append(" and product_type = #{type}");
        }
        if (beginDate != null) {
            sb.append(" and create_time >= #{beginDate}");
        }
        if (endDate != null) {
            sb.append(" and create_time <= #{endDate}");
        }
        if (userId != null) {
            sb.append(" and user_id = #{userId}");
        }
        sb.append(" order by id desc");
        if (offset != null && limit != null) {
            sb.append(" limit #{offset},#{limit}");
        }
        return sb.toString();
    }

    public String countStocks(Map<String, Object> map) {
        Integer type = (Integer) map.get("type");
        Date beginDate = (Date) map.get("beginDate");
        Date endDate = (Date) map.get("endDate");
        Long userId = (Long) map.get("userId");
        StringBuilder sb = new StringBuilder();
        sb.append("select count(1) from t_stock where 1>0 ");
        if (type != null && type > 0) {
            sb.append(" and product_type = #{type}");
        }
        if (beginDate != null) {
            sb.append(" and create_time >= #{beginDate}");
        }
        if (endDate != null) {
            sb.append(" and create_time <= #{endDate}");
        }
        if (userId != null) {
            sb.append(" and user_id = #{userId}");
        }
        return sb.toString();
    }

    public String sumStocks(Map<String, Object> map) {
        Integer type = (Integer) map.get("type");
        Date beginDate = (Date) map.get("beginDate");
        Date endDate = (Date) map.get("endDate");
        Long userId = (Long) map.get("userId");
        StringBuilder sb = new StringBuilder();
        sb.append("select sum(amount) from t_stock where 1>0 ");
        if (type != null && type > 0) {
            sb.append(" and product_type = #{type}");
        }
        if (beginDate != null) {
            sb.append(" and create_time >= #{beginDate}");
        }
        if (endDate != null) {
            sb.append(" and create_time <= #{endDate}");
        }
        if (userId != null) {
            sb.append(" and user_id = #{userId}");
        }
        return sb.toString();
    }
}
